package com.sky.controller.admin;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品接口")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 添加菜品
     * @param dishDTO
     */
    @PostMapping
    @ApiOperation("添加菜品")
    public Result addDish(@RequestBody DishDTO dishDTO){
        log.info("添加菜品参数：{}",dishDTO);
        String key = "dish_"+dishDTO.getCategoryId();
        deleteKeys(key);
        dishService.addDish(dishDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询的参数：{}",dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);

    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @ApiOperation(value = "菜品批量删除")
    @DeleteMapping
    public Result deleteDish(@RequestParam List<Long> ids){
        log.info("删除菜品的参数：{}",ids);
        deleteKeys("dish_*");
        dishService.deleteDish(ids);
        return Result.success();
    }

    /**
     * 菜品回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("菜品回显")
    public Result<DishVO> getDishMessageById(@PathVariable Long id){
        log.info("菜品回显的参数：{}",id);
        DishVO dishVo = dishService.getDishByID(id);
        return Result.success(dishVo);
    }

    /**
     * 菜品修改
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation(value = "菜品修改")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("菜品修改参数:{}",dishDTO);
        deleteKeys("dish_*");
        dishService.updateDish(dishDTO);
        return Result.success();
    }

    /**
     * 菜品起售、停售
     * @param id
     * @param status
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "菜品起售、停售")
    public Result startOrStop(Long id, @PathVariable Integer status){
        log.info("菜品起售、停售的参数：{},{}",id,status);
        deleteKeys("dish_*");
        dishService.startOrStop(id,status);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list=dishService.list(categoryId);
        return Result.success(list);
    }


    private void deleteKeys(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }


}
